import { Service } from "egg";

/**
 * Test Service
 */
export interface SearchReportSellProps {
  drug_name: string;
  standard: string;
  level: number;
  time: string;
}

export default class ReportService extends Service {
  /**
   * sayHi to you
   */
  public async getSellData(data: SearchReportSellProps) {
    const { ctx } = this;
    const _isYear = data.time.length === 4;
    let _days;
    if (!_isYear) {
      const _mounth = new Date(`${data.time}-01 00:00:00`).getMonth() + 1;
      const _year = new Date(`${data.time}-01 00:00:00`).getFullYear();
      _days = new Date(_year, _mounth, 0).getDate(); // 返回一个月有多少天
    }
    const _startTime = new Date(
      _isYear ? `${data.time}-01-01 00:00:00` : `${data.time}-01 00:00:00`
    ).getTime();
    const _endTime = new Date(
      _isYear ? `${data.time}-12-31 23:59:59` : `${data.time}-${_days} 23:59:59`
    ).getTime();
    console.log(_startTime, _endTime);
    const res = await ctx.model.WarehouseRecord.aggregate([
      {
        $match: {
          createdAt: { $gte: _startTime, $lte: _endTime },
          level: { $eq: data.level },
          "warehouse.drug.name": { $eq: data.drug_name },
          "warehouse.drug.standard": { $eq: data.standard },
          type: { $in: ["入库"] },
        },
      },
      {
        $group: {
          _id: {
            business: "$warehouse.business",
            drug_name: "$warehouse.drug.name",
            standard: "$warehouse.drug.standard",
            brand: "$warehouse.drug.brand",
            flow: "$warehouse.flow",
            level: "$level",
            type: "$type",
          },
          // 在$group添加自定义字段
          amount: {
            $sum: "$amount",
          },
        },
      },
      {
        $lookup: {
          from: "warehouserecords",
          let: {
            business: "$_id.business",
            drug_name: "$_id.drug_name",
            standard: "$_id.standard",
            brand: "$_id.brand",
            flow: "$_id.flow",
            level: "$_id.level",
            type: "入库",
          },
          pipeline: [
            {
              $match: {
                $expr: {
                  $and: [
                    { $eq: ["$warehouse._id", "$$warehouse_id"] },
                    { $eq: ["$type", "$$type"] },
                  ],
                },
              },
            },
            {
              $group: {
                _id: "$type", // 分组字段
                total: { $sum: "$amount" },
              },
            },
          ],
          as: "list",
        },
      },
      /*  {
        $project: {
          _id: 0, // 该字段查询后不可见
          business: "$_id.business", // 自定义添加字段
          drug_name: "$_id.drug_name",
          standard: "$_id.standard",
          brand: "$_id.brand",
          type: "$_id.type",
          level: "$_id.level",
          amount: 1, // 该字段查询后可见
        },
      },
      {
        $group: {
          _id: { type: "$type" },
          // 在$group添加自定义字段
          groupData: {
            $push: {
              business: "$business",
              drug_name: "$drug_name",
              brand: "$brand",
              standard: "$standard",
              amount: "$amount",
            },
          },
        },
      },
      {
        $project: {
          _id: 0, // 该字段查询后不可见
          type: "$_id.type", // 自定义添加字段
          groupData: 1, // 自定义添加字段
        },
      },*/
    ]);
    return res;
  }
}
